
var sql = require("../sql/sql");
var userSQL = require('../../config/Usersql');
var common = require("../../config/Common")

class home extends sql{
    constructor(){
        super();
    }

    // checkPassword(username,password,success_callback,error_callback){
    //     if(typeof(username) === "undefined" || username === "")
    //         return error_callback("用户名为空");
    //     else if(typeof(password) === "undefined" || username === "")
    //         return error_callback("密码为空");
            
    //     return success_callback();
    // }
   
    banner(){
        return new Promise((resolve, reject)=>{
            this.query_sql(userSQL.home.queryBanner).then(
                message=>{
                    for(var val in message){
                        message[val].pic = common.header+message[val].pic;
                    }
                    resolve(message);
                },error=>{
                    reject(error);
                }
            );
        });
    }
    
    catelist(){
    		return new Promise((resolve,reject)=>{
    			this.query_sql(userSQL.home.queryCateList).then(
    				message=>{
                        for(var val in message){
                            message[val].pic = common.header+message[val].pic;
                        }
    					resolve(message);
    				},error=>{
    					reject(error);
    				}
    			);
    		});
    }
    
    indexNews(){
    		return new Promise((resolve,reject)=>{
    			this.query_sql(userSQL.home.querynews).then(
    				message=>{
    					resolve(message);
    				},error=>{
    					reject(error);
    				}
    			);
    		});
    }

    addArticle(id,author,title,type,desc,content,click,reply){
        if(id==null || desc==null||click==null||reply==null){
            desc=null;
            var date = new Date().getTime().toString();
            console.log(date);
            id = this.MD5(date);
            console.log(id);
            click=0;
            reply=0;
        }else{
            desc=desc;
            click=click;
            reply=reply;
        }
        return new Promise((resolve,reject)=>{
            this.insert_sql(userSQL.home.insertArticle,[id,author,title,type,desc,content,parseInt(click),parseInt(reply)]).then(
                message=>{
                    resolve(message);
                },error=>{
                    reject(error);
                }
            )
        });
    }

}

module.exports = new home();